package vmcs.simulationsystem;

import java.awt.Panel;

/**
 * Title:        VMCS
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      ISS
 * @author Pang Pingli
 * @version 1.0
 */
import java.awt.event.ActionListener;
import java.awt.*;

import vmcs.common.VMCSException;
import vmcs.store.StoreItem;
import vmcs.store.StoreObject;

public class ButtonItemDisplay extends Panel {

  private ButtonItem       items[];
  private int              len;
  private Label            lb;

  public ButtonItemDisplay(String title, StoreItem sitem[], int length) {

    len = length;
    System.out.println("ButtonItemDisplay:" + len);
    Panel   tp1 = new Panel();

    lb = new Label(title);
    lb.setFont(new Font ("Helvetica", Font.BOLD, 12));
    tp1.add(lb);

    this.setLayout(new GridLayout(0,1));

    this.add(tp1);

    int i;
    items = new ButtonItem[len];

    GridLayout  lm = new GridLayout(1,2);
    StoreObject       ob;

    for (i = 0; i < len; i++){
      ob = sitem[i].getContent();

      items[i] = new ButtonItem(ob.getName(), ButtonItem.GRID);
      this.add(items[i]);
    }

  }

  public void  addListener(ActionListener  l){
    int         i;
    for(i =0; i<  len; i++){
      items[i].addListener(l);
      items[i].setActionCommand(String.valueOf(i));
    }
  }

  public void setActive(boolean st){
    int       i;
    lb.setEnabled(st);
    for(i = 0; i < len; i++){
      items[i].setActive(st);
    }
  }

  public void clear(){
      int         i;
    for(i =0; i<  len; i++){
      items[i].clear();
    }
  }


  public void displayQty(int idx, int qty)
  throws VMCSException {
    System.out.println("ButtonItemDisplay:" + idx);
    if (idx >= len)
      throw new VMCSException ("ButtomDisplay.setQty", "Index over flow");

    items[idx].setValue(qty);
  }

}
